package com.example.common.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;

/**
 * @program: springsecurity-study
 * @description: SpringSecurity配置Bean的类
 * @author: ChenZhiXiang
 * @create: 2019-07-26 21:54
 **/
@Component
public class SpringSecurityBean {

    /** 数据源 */
    @Autowired
    private DataSource dataSource;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /** 记住我信息入库的接口 */
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        // 第一次是数据库还没有记住我相关的表，这句用于建表，之后需要注释了
        //tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }

}
